package com.scy.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class RenameOriginWarFileAndDir {
    public static void main(String[] args) throws Exception{
        System.out.println("----- com.scy.servlet.RenameOriginWarDir类中的main方法执行了 -------");

        System.out.println("获取当前的classpath:"+System.getProperty("java.class.path"));

        System.out.println(args[0]);
        System.out.println(args[1]);
        String dirPath = args[0] + System.getProperty("file.separator") + args[1];
        File file = new File(dirPath);

        file.renameTo(new File(file.getAbsoluteFile() + "_originCopy"));

        System.out.println("原始web目录已经更名为:"+file.getAbsoluteFile() + "_originCopy");

        FileInputStream fileInputStream = new FileInputStream(dirPath + ".war");

        FileOutputStream fileOutputStream = new FileOutputStream(dirPath + "originWarCopy.war");

        byte[] buffer = new byte[1024 * 10];

        int len = 0;
        while((len = fileInputStream.read(buffer)) != -1){
            fileOutputStream.write(buffer, 0, len);
        }

        fileOutputStream.close();
        fileInputStream.close();

        System.out.println("原始war包已经备份完成");

    }

    public static void deleteFolder(File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    deleteFolder(file);
                }
            }
        }
        folder.delete();
    }
}
